/*
 * @Description: 图片列表显示
 * @Version: V1.0.0
 * @Author: 黄鑫
 * @Date: 2025-05-07
 * @LastEditors: 黄鑫
 * @LastEditTime: 2025-07-23
 * @FilePath: hx_imagelistview.dart
 * @Remarks: 
 */

import 'package:extended_image/extended_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:hx_base/pages/imagelistview/hx_image_preview_page.dart';

class HxImagelistview<T> extends StatelessWidget {
  /// 图片数组、T是自定义model 必填
  final List<T> list;

  /// 外部决定自定义model的图片url 必填
  final String Function(T item) getImageUrl;

  /// 组件外边距
  final EdgeInsetsGeometry? margin;

  /// 组件内边距
  final EdgeInsetsGeometry? padding;

  /// 组件样式
  final BoxDecoration? decoration;

  /// 横向距离 默认10
  final double? spacing;

  /// 纵向距离 默认10
  final double? runSpacing;

  ///图片圆角
  final BorderRadius? itemRadius;

  ///图片边框
  final Border? itemBorder;

  /// 宽度 必填
  final double width;

  /// 高度 必填
  final double height;

  const HxImagelistview({
    super.key,
    required this.list,
    required this.getImageUrl,
    required this.width,
    required this.height,
    this.itemRadius,
    this.itemBorder,
    this.margin,
    this.padding,
    this.spacing,
    this.runSpacing,
    this.decoration,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      margin: margin,
      padding: padding,
      decoration: decoration,
      child: Wrap(
        spacing: spacing ?? 10,
        runSpacing: runSpacing ?? 10,
        children: list.map(
          (e) {
            final imageUrl = getImageUrl(e);
            return GestureDetector(
              onTap: () => Get.to(
                () => HxImagePreviewPage(imageUrl),
                opaque: false,
                fullscreenDialog: true,
              ),
              child: ExtendedImage.network(
                imageUrl,
                width: width,
                height: height,
                fit: BoxFit.cover,
                border: itemBorder,
                borderRadius: itemRadius,
                cache: true,
                loadStateChanged: (state) {
                  if (state.extendedImageLoadState == LoadState.loading) {
                    return Container(
                        width: double.infinity,
                        height: double.infinity,
                        color: const Color(0xFFEAEAEA),
                        child: const CupertinoActivityIndicator());
                  }
                  if (state.extendedImageLoadState == LoadState.failed) {
                    return Container(
                      width: double.infinity,
                      height: double.infinity,
                      alignment: Alignment.center,
                      color: const Color(0xFFEAEAEA),
                      child: Text(
                        "加载失败",
                        style: TextStyle(
                          fontSize: 10,
                        ),
                      ),
                    );
                  }
                  return null;
                },
              ),
            );
          },
        ).toList(),
      ),
    );
  }
}
